<template>
  <!-- 用户 -->
  <el-dialog v-dialogDrag v-dialogDragWidth title="选择销售人员" :visible.sync="visible" width="800px" top="5vh" append-to-body>

    <!-- 查询条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="销售人员" prop="nickName">
        <el-select
          filterable
          v-model="queryParams.nickName"
          placeholder="销售人员"
          clearable
          size="small"
        >
          <el-option
            v-for="item in salesList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.nickName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="sysNormalDisable">
        <el-select v-model="queryParams.sysNormalDisable" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 用户列表 -->
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="salesList" @selection-change="handleSelectionChange" height="260px">

        <el-table-column label="序号" align="center" prop="orderNum" />
        <el-table-column label="销售人员" align="center" prop="nickName" />
        <el-table-column label="人员分组" align="center" prop="comnDictUserGroup">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.comn_dict_user_group" :value="scope.row.comnDictUserGroup"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="sysNormalDisable">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.sysNormalDisable"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="success" plain
              size="small"
              icon="el-icon-check"
              @click="selectSalesPerson_done(scope.row)"
            ><span> 选择</span></el-button>
          </template>
        </el-table-column>

      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

    <div slot="footer" class="dialog-footer">
<!--      <el-button type="primary" @click="handleSelectSalesPerson">确 定</el-button>-->
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { listUserGroup } from "@/api/comn/userGroup";
export default {
  dicts: ['sys_normal_disable','comn_dict_user_group'],
  props: {
    // 角色编号
    roleId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      userIds: [],
      // 总条数
      total: 0,
      // 销售人员列表
      salesList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        userName: undefined,
        phonenumber: undefined
      },
      selectSalesPerson: {
        id: null,
        nickName: null,
        userName: null
      }
    };
  },
  methods: {
    // 显示弹框
    show() {
      // this.queryParams.roleId = this.roleId;
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.userId);
    },
    // 查询表数据
    getList() {
      listUserGroup().then(response => {
          this.salesList = response.rows
        });
      },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },


    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 选择用户：完成 */
    selectSalesPerson_done( user ) {
      this.$emit("ok", user );
      this.visible = false;
    },


    /** 选择授权用户操作 */
    handleSelectSalesPerson() {
      const roleId = this.queryParams.roleId;
      const userIds = this.userIds.join(",");
      this.$emit("ok");
    }
  }
};
</script>
